package com.uzhie.media.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.util.CacheManager;

/**
 * Created by TangFei on 2018/8/12.
 * 调用流媒体服务器查询在线通道数
 */
public class MediaOnLineNumTask<V> extends CallableTask<Integer, JSON> {

    private String simNo;

    private int i = 0;

    public MediaOnLineNumTask(JSONObject object, CacheManager<String, JSON> cacheManager) {
        super.cacheManager = cacheManager;
        super.object = object;
        this.simNo = object.getString("simNo");
    }

    @Override
    public Integer call() throws Exception {
        Integer number = -1;
        do {
            JSON json = cacheManager.get(CacheManager.DES.SYS_CACHE, simNo, JSON.class);
            if (json instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) json;
                number = jsonObject.getJSONObject("result").getJSONObject("data").getInteger("number");
            }
            number = (null == number) ? -1 : number;
            i++;
            Thread.sleep(200);
        } while (number < 0 && i < 50);
        cacheManager.remove(CacheManager.DES.SYS_CACHE, simNo);
        logger.info("2.5 SIM卡号：" + simNo + " 获取到的通道数 " + number + " 执行次数 " + i);
        return number == -1 ? 0 : number;
    }

}
